package com.soul.doc.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.soul.common.msg.ApiResult;
import com.soul.doc.bean.dto.CollectionDto;
import com.soul.doc.bean.dto.SongListAddDto;
import com.soul.doc.bean.dto.SongListSearchDto;
import com.soul.doc.bean.dto.SongListUpdateDto;
import com.soul.doc.bean.vo.SongListVo;
import com.soul.doc.service.SongListService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.soul.common.controller.BaseController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 歌单表 前端控制器
 * </p>
 *
 * @author sws
 * @since 2023-11-27 23:13:16
 */
@RestController
@RequestMapping("/song/list")
@Api(value="songList", tags = "歌单接口")
public class SongListController extends BaseController {

    @Autowired
    private SongListService songListService;

    @GetMapping("/{page}/{size}/list/ext")
    public ApiResult<IPage<SongListVo>> querySongList(@PathVariable("page") Integer page, @PathVariable("size") Integer size, SongListSearchDto searchDto){
        IPage<SongListVo> songListVoIPage = this.songListService.querySongList(page, size, searchDto);
        return ApiResult.success(songListVoIPage);
    }


    @GetMapping("/my/list")
    public ApiResult<List<SongListVo>> queryMySongList(SongListSearchDto searchDto){
        List<SongListVo> songListVos = this.songListService.queryMySongList();
        return ApiResult.success(songListVos);
    }
    @GetMapping("/love/list")
    public ApiResult<List<SongListVo>> queryMyLoveList(SongListSearchDto searchDto){
        List<SongListVo> songListVos = this.songListService.queryMyLoveList();
        return ApiResult.success(songListVos);
    }
    @PostMapping("/add")
    public ApiResult<?> addSongList(@RequestBody SongListAddDto addDto){
        this.songListService.addSongListByUser(addDto);
        return ApiResult.success();
    }

    /**
     * 收藏歌单
     * @param collectionDto
     * @return
     */
    @PostMapping("/add/love")
    public ApiResult<?> addLove(@RequestBody CollectionDto collectionDto){
        this.songListService.addLove(collectionDto);
        return ApiResult.success();
    }

    /**
     * 收藏歌曲
     * @param collectionDto
     * @return
     */
    @PostMapping("/add/music")
    public ApiResult<?> addMusic(@RequestBody CollectionDto collectionDto){
        this.songListService.addMusic(collectionDto);
        return ApiResult.success();
    }

    @GetMapping("/{id}/info/ext")
    public ApiResult<SongListVo> getInfo(@PathVariable("id") Long id){
        SongListVo listVo = songListService.getInfoById(id);
        return ApiResult.success(listVo);
    }

    @PostMapping("/add/{type}/num/{songListId}/ext")
    public ApiResult addMusicPlayNum(@PathVariable("songListId") Long songListId,@PathVariable("type") String type){
        if (ObjectUtil.equals(type,"play")){
            songListService.addPlayNum(songListId);
        }else if (ObjectUtil.equals(type,"collect") && songListId != 0L){
            songListService.addCollectNum(songListId,"add");
        }
        return ApiResult.success();
    }

    @PutMapping("/update")
    public ApiResult updateSongList(@RequestBody SongListUpdateDto updateDto){
        songListService.updateSongListByUser(updateDto);
        return ApiResult.success();
    }
    @PostMapping("/delete/{id}")
    public ApiResult updateSongList(@PathVariable("id") Long id){
        songListService.deleteSongList(Collections.singletonList(id));
        return ApiResult.success();
    }
}
